import LocalStorage from "./LocalStorage";

export default class LocalValue {

    protected value: any;
    protected key: string;
    protected readLocal: boolean = true;
    constructor(key, value, readLocal = true) {
        this.key = key;
        this.readLocal = readLocal;
        this.loadValue(value);
        // cc.log(" LocalValue constructor ", this.value);
    }

    protected loadValue(initValue: any) {
        if (this.readLocal) {
            let localValue = LocalStorage.getJsonBase64(this.key);
            if (localValue == null || localValue == undefined) {
                this.setValue(initValue);
            } else {
                this.value = localValue;
            }

        } else {
            this.value = initValue;
        }
    }

    protected saveValue() {
        LocalStorage.setJsonBase64(this.key, this.value);
    }



    getValue() {
        // cc.log(" getValue ", this.value);
        return this.value;
    }

    setValue(value,save:boolean = true) {
        this.value = value;
        if (this.readLocal && save) {
            this.saveValue();
        }
    }




}
